﻿'Public Class TokenParser(Of TToken)
'    Private m_productionManager As ProductionManager

'    Private m_scanner As IScanner(Of TToken)
'    Private m_stateStack As Stack(Of StackEntry)
'    Private m_tokenMapping As TokenMapping(Of TToken)

'    Protected ReadOnly Property StackTopState() As Integer
'        Get
'            Debug.Assert(m_stateStack.Count > 0)
'            Return m_stateStack.Peek().StateIndex
'        End Get
'    End Property

'    Protected Sub PushState(ByVal stateIndex As Integer, ByVal syntaxObject As Object)
'        m_stateStack.Push(New StackEntry With {.StateIndex = stateIndex, .SyntaxObject = syntaxObject})
'    End Sub

'    Protected Function PopState() As StackEntry
'        Return m_stateStack.Pop
'    End Function

'    Private Sub Bar()

'        'set current state to token mapping
'        m_tokenMapping.CurrentStateIndex = StackTopState

'        'peek one token
'        Dim peekToken = m_scanner.Peek()

'        'get the shift 
'        Dim shift = m_tokenMapping.GetShift(peekToken)

'        If shift Is Nothing Then
'            'try reduce
'            Dim reduce = m_tokenMapping.GetReduce(peekToken)

'            If reduce Is Nothing Then
'                'TODO: Error handling
'                Throw New NotImplementedException("Error handling is not implemented")
'            End If

'            DoReduce(reduce)
'        End If

'        DoShift(shift)
'    End Sub

'    Private Sub DoReduce(ByVal reduce As Reduce)
'        Dim production = reduce.ReduceProduction
'        Dim reduceRule = production.SemanticRule

'        'pop all the states from stack in sequence
'        Dim listOfResults As New List(Of Object)(production.Sequence.Count)

'        For Each symbol In production.Sequence
'            Dim entry = PopState()
'            listOfResults.Add(entry.SyntaxObject)
'        Next

'        Dim context As New ParserContext(production, listOfResults)

'        'run the reduce rule
'        Dim reducedObject As Object = Nothing
'        If reduceRule IsNot Nothing Then
'            reducedObject = reduceRule(context)
'        End If

'        'find the current top-stack state
'        m_tokenMapping.CurrentStateIndex = StackTopState


'    End Sub

'    Private Sub DoShift(ByVal shift As LR1Shift)
'        Dim shiftToken = m_scanner.Read
'        Dim nextState = shift.TargetStateIndex

'        If nextState = LR1TransitionTable.AcceptStateIndex Then
'            Accept()

'            Exit Sub
'        End If

'        'push state into stack
'        PushState(nextState, shiftToken)
'    End Sub

'    Private Sub Accept()
'        'finish parsing, raise finish event
'    End Sub

'    'Private Sub Foo()
'    '    Dim currentStateIndex = m_stateStack.Peek() 'Get the stack top
'    '    Dim currentState = m_productionManager.TransitionTable(currentStateIndex)

'    '    'Check action for pending symbol
'    '    Dim nextAction = currentState.Actions(m_pendingSymbol)

'    '    If nextAction Is Nothing Then
'    '        'TODO: Error handling
'    '        Throw New NotImplementedException("Error handling is not implemented")
'    '    End If

'    '    If TypeOf nextAction Is LR1Shift Then
'    '        'Shift scanner stream
'    '        Dim shift = TryCast(nextAction, LR1Shift)

'    '    ElseIf TypeOf nextAction Is Reduce Then
'    '        'reduce current production
'    '        Dim reduce = TryCast(nextAction, Reduce)

'    '    Else
'    '        'TODO: goto
'    '    End If
'    'End Sub

'    Protected Structure StackEntry
'        Public StateIndex As Integer
'        Public SyntaxObject As Object
'    End Structure
'End Class
